
package com.joysuch.wwyt.core.common.ajax;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.joysuch.wwyt.core.enums.Status;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiModel(description = "接口返回数据")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultBeanPlus<T> {

    public static final int SUCCESS_CODE = 0;

    @ApiModelProperty(value = "响应状态码")
    private int code;
    @ApiModelProperty(value = "响应信息")
    private String msg;
    @ApiModelProperty(value = "响应对象")
    private T data;
    @ApiModelProperty(value = "列表数据长度")
    private Long length;

    public ResultBeanPlus(int resultCode, String msg, T data) {
        super();
        this.code = resultCode;
        this.msg = msg;
        this.data = data;
    }

    public ResultBeanPlus() {
        super();
    }

    /**
     * 返回一个带错误提示的错误结果
     *
     * @param code
     *            错误码
     * @param message
     *            错误提示
     * @return 错误结果
     */
    public static ResultBeanPlus wrapErrorResult(int code, String message) {
        ResultBeanPlus r = new ResultBeanPlus();
        r.setCode(code);
        r.setMsg(message);
        return r;
    }

    /**
     * 返回一个默认错误结果
     *
     * @param message
     *            错误提示
     * @return 错误结果
     */
    public static ResultBeanPlus wrapErrorResultDefault(String message) {
        ResultBeanPlus r = new ResultBeanPlus();
        r.setCode(-90);
        r.setMsg(message);
        return r;
    }

    /**
     * 返回默认成功结果，不包含数据
     *
     * @return 默认成功结果
     */
    public static ResultBeanPlus defaultSuccessResult() {
        ResultBeanPlus result = new ResultBeanPlus();
        result.code = SUCCESS_CODE;
        return result;
    }

    public static <T> ResultBeanPlus success(T data) {
        return new ResultBeanPlus(SUCCESS_CODE, null, data);
    }

    public static <T> ResultBeanPlus success(T data, String msg) {
        return new ResultBeanPlus(SUCCESS_CODE, msg, data);
    }

    public static <T> ResultBeanPlus fail(int code, String msg, T data) {
        return new ResultBeanPlus(code, msg, data);
    }

    public static ResultBeanPlus fail(int code, String msg) {
        return new ResultBeanPlus(code, msg, null);
    }

    public static ResultBeanPlus fail(Status status) {
        return new ResultBeanPlus(status.getCode(), status.getMsg(), null);
    }

    public static Map<String, Object> successInMap(Object data) {
        return toMap(SUCCESS_CODE, null, data);
    }

    public static Map<String, Object> successInMap(Object data, String msg) {
        return toMap(SUCCESS_CODE, msg, data);
    }

    public static Map<String, Object> failInMap(int code, String msg, Object data) {
        return toMap(code, msg, data);
    }

    public static Map<String, Object> failInMap(int code, String msg) {
        return toMap(code, msg, null);
    }

    static Map<String, Object> toMap(int code, String msg, Object data) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", code);
        if (msg != null) {
            map.put("msg", msg);
        }
        if (data != null) {
            map.put("data", data);
        }
        return map;
    }

    public static <T> ResultBeanPlus<T> pageData(List<T> list, long total) {
        ResultBeanPlus rb = new ResultBeanPlus();
        rb.setData(list);
        rb.setLength(total);
        rb.setCode(SUCCESS_CODE);
        return rb;
    }

    public static ResultBeanPlus pageData(Page p) {
        ResultBeanPlus rb = new ResultBeanPlus();
        rb.setData(p.getContent());
        rb.setLength(p.getTotalElements());
        rb.setCode(SUCCESS_CODE);
        return rb;
    }

}
